package com.infosys.setl.fp.demo1;

//Java 语言中的函数编程
//利用闭包和高阶函数编写模块化的 Java 代码
//http://www.ibm.com/developerworks/cn/java/j-fp/
import java.util.Comparator;

import org.apache.commons.functor.BinaryPredicate;
import org.apache.commons.functor.core.comparator.IsGreaterThanOrEqual;

public class TestA {
    public static void main(String[] args) {
        try {
            Comparator<SetlItem> priceComparator = new PriceComparator();
            //表达式特化
            BinaryPredicate<SetlItem, SetlItem> binaryPredicate = new IsGreaterThanOrEqual<SetlItem>(priceComparator);
            //test
            SetlItem item1 = new SetlItem("item1");
            item1.setPrice(100);
            SetlItem item2 = new SetlItem("item2");
            item2.setPrice(99);
            SetlItem item3 = new SetlItem("item3");
            item3.setPrice(101);
            if (binaryPredicate.test(item1, item2)) {
                System.out.println("Item1 costs more than Item2!");
            } else {
                System.out.println("Item2 costs more than Item1!");
            }
            if (binaryPredicate.test(item1, item3)) {
                System.out.println("Item1 costs more than Item3!");
            } else {
                System.out.println("Item3 costs more than Item1!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}